package jeconbond.experiment.process;

import java.util.ArrayList;
import java.util.List;

import laboratoryQ.experiment.process.BaseExperimentContext;
import jeconbond.economic.agent.service.IServiceAgent;
import jeconbond.economic.agent.service.NextStepCommandAgent;
import jeconbond.economic.agent.AutomataActivityListener;
import jeconbond.economic.systems.IResourceRedestributionSystem;
import jeconbond.automata.behaviour.IAutomataWalkListener;
import jeconbond.experiment.BaseJEBExperimentSettings;

public abstract class BaseJEBExperimentContext extends BaseExperimentContext implements IJEBExperimentContext {
	private NextStepCommandAgent nextStepCommandAgent;
	private IAutomataWalkListener automataWalkListener;
	private IResourceRedestributionSystem rrs;
	private List<IServiceAgent> serviceAgents = new ArrayList<IServiceAgent>();
	
	// 4 create as factory
	protected BaseJEBExperimentContext() { super(null); }

	public BaseJEBExperimentContext(BaseJEBExperimentSettings settings) {
		super(settings);
		rrs = settings.rrsFactory.newRRS(this);
		nextStepCommandAgent =
			new NextStepCommandAgent(settings.stepSize);
		
		automataWalkListener =
			new AutomataActivityListener(nextStepCommandAgent.stepCounter);
	}

	@Override
	public IAutomataWalkListener getAutomataWalkListener() {
		return automataWalkListener;
	}

	@Override
	public NextStepCommandAgent getNextStepCommander() {
		return nextStepCommandAgent;
	}

	@Override
	public BaseJEBExperimentSettings getExperimentSettings() {
		return (BaseJEBExperimentSettings) super.getExperimentSettings();
	}

	@Override
	public IResourceRedestributionSystem getRRS() {
		return rrs;
	}

	@Override
	public IServiceAgent[] getServiceAgents() {
		IServiceAgent[] result = new IServiceAgent[serviceAgents.size() + 1]; 
		serviceAgents.toArray(result);
		result[result.length - 1] = nextStepCommandAgent;
		return result;
	}
	
	@Override
	public void addServiceAgent(IServiceAgent serviceAgent) {
		serviceAgents.add(serviceAgent);
	}
}
